package com.sweets.look.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;

import com.sweets.look.util.ViewUtils;

public class MarkSlidingView extends HorizontalScrollView {
    public interface OnMenuOpenListener {
        void onMenuOpen(int arg1);
    }

    private boolean isOperateLeft;
    private boolean isOperateRight;
    private int mHalfMenuWidth;
    private int mMenuWidth;
    private OnMenuOpenListener mOnMenuOpenListener;
    private final int mScreenHeight;
    private final int mScreenWidth;
    private boolean once;

    public MarkSlidingView(Context context) {
        this(context, null, 0);
    }

    public MarkSlidingView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (ViewUtils.getWindowHeight(context) >= ViewUtils.getWindowWidth(context)) {
            this.mScreenWidth = ViewUtils.getWindowWidth(context);
            this.mScreenHeight = ViewUtils.getWindowHeight(context);
        } else {
            this.mScreenWidth = ViewUtils.getWindowHeight(context);
            this.mScreenHeight = ViewUtils.getWindowWidth(context);
        }
    }

    public MarkSlidingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            this.scrollTo(this.mMenuWidth, 0);
            this.once = true;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!this.once) {
            ViewGroup v3 = (ViewGroup) this.getChildAt(0);
            View v1 = v3.getChildAt(0);
            View v0 = v3.getChildAt(1);
            View v2 = v3.getChildAt(2);
            this.mMenuWidth = (this.mScreenHeight - this.mScreenWidth) / 2;
            this.mHalfMenuWidth = this.mMenuWidth / 2;
            v1.getLayoutParams().width = this.mMenuWidth;
            v0.getLayoutParams().width = this.mScreenWidth;
            v2.getLayoutParams().width = this.mMenuWidth;
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (l > this.mMenuWidth) {
            this.isOperateRight = true;
            this.isOperateLeft = false;
        } else {
            this.isOperateRight = false;
            this.isOperateLeft = true;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP: {//1
                int v1 = this.getScrollX();
                if (this.isOperateLeft) {
                    if (v1 > this.mHalfMenuWidth) {
                        this.smoothScrollTo(this.mMenuWidth, 0);
                    } else {
                        this.smoothScrollTo(this.mMenuWidth, 0);
                        if (this.mOnMenuOpenListener != null) {
                            this.mOnMenuOpenListener.onMenuOpen(0);
                        }
                    }
                }

                if (!this.isOperateRight) {
                    return true;
                }

                if (v1 > this.mHalfMenuWidth + this.mMenuWidth) {
                    this.smoothScrollTo(this.mMenuWidth, 0);
                    if (this.mOnMenuOpenListener == null) {
                        return true;
                    }

                    this.mOnMenuOpenListener.onMenuOpen(1);
                    return true;
                }

                this.smoothScrollTo(this.mMenuWidth, 0);
                break;
            }
            default: {
                return super.onTouchEvent(ev);
            }
        }

        return true;
    }

    public void setOnMenuOpenListener(OnMenuOpenListener mOnMenuOpenListener) {
        this.mOnMenuOpenListener = mOnMenuOpenListener;
    }
}

